/*
 * @Author: szx
 * @Date: 2021-11-27 12:49:30
 * @LastEditTime: 2021-11-27 13:04:43
 * @Description:
 * @FilePath: \leetcode\200-299\202\202.js
 */
/**
 * @param {number} n
 * @return {boolean}
 */
// var isHappy = function (n) {
//     const st = new Set();
//     while (n != 1) {
//         let sum = 0;
//         while (n) {
//             let i = Math.floor(n % 10);
//             sum += i * i;
//             n = Math.floor(n / 10);
//         }
//         n = sum;
//         console.log(n);
//         if (st.has(n)) break;
//         st.add(n);
//     }
//     return n == 1;
// };

var isHappy = function (n) {
    const getNext = (n) => {
        let sum = 0;
        while (n) {
            sum += (n % 10) * (n % 10);
            n = Math.floor(n / 10);
        }
        return sum;
    };

    let slow = n;
    let fast = getNext(n);
    while (fast != 1 && slow != fast) {
        console.log(slow, fast);
        slow = getNext(slow);
        fast = getNext(getNext(fast));
    }
    return fast == 1;
};
console.log(isHappy(1));
